import {defineStore} from 'pinia'
import {RouteMeta} from 'vue-router'
import {unique} from '@/utils/aryutils'

interface IRouteType{
    tabAry:Array<RouteMeta>
}
const useTabStore=defineStore('tabs',{
    state:():IRouteType=>{
        return{
            tabAry:[]
        }
    },
    getters:{
        getTabAry(state){
            return state.tabAry
        }
    },
    actions:{
        addTabs(item:RouteMeta){
           this.tabAry.push(item)
           //删除掉数组中的空对象
           //var filterNullAry = this.tabAry.filter(value => Object.keys(value).length !== 0);
           let index=0
           this.tabAry.forEach((value:RouteMeta,i:number)=>{
                if( Object.keys(value).length==0){
                    index=i;
                }
           })
           this.tabAry[index]={title:'工作台',path:'/home/workplace'}
           //按照对象属性去重
           let uniqueAray= unique(this.tabAry,"title")
           this.tabAry=uniqueAray
        },
        removeTab(title:string){
            const ary=this.tabAry.filter((item:RouteMeta)=>{
                return item.title!=title
            })

            this.tabAry=ary
        }
    }
})
export default useTabStore